// pages/resultype/glaucoma/glaucoma.js
const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const jsonData = require("../jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		leadName: '',
		addOrMod:'add',
		//选项集
		anteriorChamberIndex:-1,
		eyeTouchIndex:-1,
		suspectedGlaucomaIndex:-1,
		result:[],
		suspectedGlaucomaIndex:-1,
		anteriorChamberArray:jsonData.anteriorChamberArray,
		noneOrBothOrLeftOrRight:jsonData.noneOrBothOrLeftOrRight,
		normalOrAbnormal:jsonData.normalOrAbnormal,
    schoolId:'',
		/**青光眼**/
		//周边前房
		anteriorChamber: {
			new_scid: null,
			new_categoryid: 'LB0004',
			new_itemid: 'XM0024',
			new_contentid: 'NR0092',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//眼球触摸
		eyeTouch: {
			new_scid: null,
			new_categoryid: 'LB0004',
			new_itemid: 'XM0022',
			new_contentid: 'NR0095',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//眼球触摸具体描述
		eyeTouchDescription: {
			new_scid: null,
			new_categoryid: 'LB0004',
			new_itemid: 'XM0022',
			new_contentid: 'NR0097',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//体征询问
		physicalSignsInquiry: {
			new_scid: null,
			new_categoryid: 'LB0004',
			new_itemid: 'XM0008',
			new_contentid: 'NR0098',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//疑似青光眼
		suspectedGlaucoma: {
			new_scid: null,
			new_categoryid: 'LB0004',
			new_itemid: 'XM0030',
			new_contentid: 'NR0101',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getGlaucomaResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
		}
		
	},
	//提交表单
	glaucomaSubmit: function(e) {
		var createResult = this.getGlaucoma();
		var that = this;
		commonutil.createScreenResults(createResult,function(res){
			var idArray = res.result.split('|');
			for(var i = 0;i < idArray.length;i++){
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				anteriorChamber:	createResult[0],
				eyeTouch:	createResult[1],
				eyeTouchDescription:	createResult[2],
				physicalSignsInquiry:	createResult[3],
				suspectedGlaucoma:	createResult[4],
			});
		});
	},
	//更新
	glaucomaUpdate:function(){
		var updateResult = this.getGlaucoma();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getGlaucomaResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title:'加载中...',
			mask:true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if(reusltArray.length==0){
			wx.hideLoading();
			return ;
		}
		this.setData({
			addOrMod:'mod'
		})
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0092') {
				this.setData({
					anteriorChamber: element
				})
			} else if (element.new_contentid === 'NR0095') {
				this.setData({
					eyeTouch: element
				})
			} else if (element.new_contentid === 'NR0097') {
				this.setData({
					eyeTouchDescription: element
				})
			} else if (element.new_contentid === 'NR0098') {
				var optionsArray = element.new_result.split(',');
				this.setData({
					physicalSignsInquiry: element,
					result:optionsArray,
				})
			} else if (element.new_contentid === 'NR0101') {
				this.setData({
					suspectedGlaucoma: element
				})
			}
		}
		wx.hideLoading();
	},
	/**选项集、文本赋值**/
	onAnteriorChamberChange: function(e) {
		this.setData({
			anteriorChamberIndex: e.detail.index
		})
	},
	onEyeTouchChange: function(e) {
		var result = 'eyeTouch.new_result';
		this.setData({
			[result]: '',
			eyeTouchIndex: e.detail.index
		})
	},
	onEyeTouchDescriptionChange: function(e) {
		var result = 'eyeTouchDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onPhysicalSignsInquiryChange: function(e) {
		this.setData({
			result:e.detail
		})
	},
	onSuspectedGlaucomaChange: function(e) {
		this.setData({
			suspectedGlaucomaIndex: e.detail.index
		})
	},
	/**获取createResult**/
	getGlaucoma: function() {
		var createResult = [];
		var scid = this.data.scid;
		var customerid = this.data.customerid;
		var anteriorChamber = this.data.anteriorChamber;
		var eyeTouch = this.data.eyeTouch;
		var eyeTouchDescription = this.data.eyeTouchDescription;
		var physicalSignsInquiry = this.data.physicalSignsInquiry;
		var suspectedGlaucoma = this.data.suspectedGlaucoma;

		//周边前房
		var anteriorChamberIndex = this.data.anteriorChamberIndex;
		var anteriorChamberArray = this.data.anteriorChamberArray;
		if (anteriorChamberIndex >-1) {
			anteriorChamber.new_result = anteriorChamberArray[anteriorChamberIndex];
		}
		//眼球触摸
		var eyeTouchIndex = this.data.eyeTouchIndex;
		var normalOrAbnormal = this.data.normalOrAbnormal;
		if (eyeTouchIndex>-1) {
			eyeTouch.new_result = normalOrAbnormal[eyeTouchIndex];
		} 
		//体征询问
		var selected = this.data.result;
		var str = '';
		if(selected!=null&&selected.length>0){
		  str = selected.join(",");
		}	
		physicalSignsInquiry.new_result = str;
		//suspectedGlaucomaIndex
		var suspectedGlaucomaIndex = this.data.suspectedGlaucomaIndex;
		var noneOrBothOrLeftOrRight = this.data.noneOrBothOrLeftOrRight;
		if (suspectedGlaucomaIndex >-1) {
			suspectedGlaucoma.new_result = noneOrBothOrLeftOrRight[suspectedGlaucomaIndex];
		}

		createResult.push(anteriorChamber);
		createResult.push(eyeTouch);
		if(eyeTouch.new_result=='SC0000000005'&&eyeTouchDescription.new_result!=''){
			createResult.push(eyeTouchDescription);
		}
		if(str!=''){
			createResult.push(physicalSignsInquiry);
		}	
		createResult.push(suspectedGlaucoma);
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	}

})
